package com.star.batchhandler.demo;

import java.sql.ResultSet;
import java.sql.SQLException;

import javax.sql.DataSource;

import com.star.platform.transaction.inter.ITransactionService;

public abstract class AbstractJob implements Runnable{
	public static volatile long consumTime = 0;
	public static volatile long consumAllTime = 0;
	public static volatile int random = 0;
	protected static volatile Object timeLock = new Object();
	
//	protected String deleteAadmin = "delete from admin where id > 320624";
	protected String selectSEQ = "select seq_admin.nextval from dual";
	protected String selectSQL = "select * from admin t where id < 95200";//"left join admin_role r on t.cid = r.id"
//	protected String selectSQL = "select sum(cid) from admin t full join admin_role r on t.cid = r.id where t.id < 320624";
	
	protected DataSource ds;
	protected ITransactionService tx;

	protected String getInsertSQL(){
		return "insert into admin (id,username,cid,pid) values (?,'TEST"+(random++)+"',"+(900+(int)(Math.random()*1000))+","+(49100+(int)(Math.random()*900))+")";
	}
	protected String getUpdateSQL(){
		return "update admin a set username = 'GOOD00"+(random++)+"',cid = "+(900+(int)(Math.random()*1000))
				+",pid = "+(49100+(int)(Math.random()*900))+" where a.id < 5200" /*+ (4100+(int)(Math.random()*100))*/;
//		return "update admin a set username = 'GOOD00"+(random++)+"',cid = "+(900+(int)(Math.random()*1000))+" where a.id =" + (4100+(int)(Math.random()*100));
	}
	public AbstractJob(DataSource ds, ITransactionService tx) {
		this.ds = ds;
		this.tx = tx;
	}
	
	protected void checkGT_1(int re) {
		if (re < 0) {
			throw new IllegalStateException("update error!");
		}
	}
	protected void checkLT_2(int re) {
		if (re < -2) {
			throw new IllegalStateException("update error!");
		}
	}

	protected void checkTRUE(boolean bool) {
		if (!bool) {
			throw new IllegalStateException("execute error!");
		}
	}

	protected long checkRS1(ResultSet re, String column) throws SQLException {
		long id = 1;
//		if(re.next()){
//			id = re.getInt(1);
//		}else{
//			throw new IllegalStateException("no row!");
//		}
//		while(re.next()){
		while(re.next()){
		for (int i=1;i<= re.getMetaData().getColumnCount();i++) {
			re.getObject(i);
		}	
		}
//		}
		
//		if (re.next()) {
//			throw new IllegalStateException("more than one row!");
//		}
		re.close();
		return id;
	}
	
	protected long getId(ResultSet re, String column) throws SQLException{
		long id = -1;
		if(re.next()){
			id = re.getInt(1);
		}else{
			throw new IllegalStateException("no row!");
		}
//		if (re.next()) {
//			throw new IllegalStateException("more than one row!");
//		}
		re.close();
		return id;
	}

}
